<template>
  <div>
    <div v-if="!pushStatus">
    <el-row style="margin-bottom:10px">
      <el-col :span="8" >
        <h4 style="margin:0">
          <span>活动列表</span>
          <el-button  type="success" class="addBtn" v-if="userRole.HPT_AC_1706001" @click="addUserFun('')">添加活动</el-button>          
        </h4>
      </el-col>
      <el-col :span="16" style="text-align:right">
        
      </el-col>
    </el-row>
   
    <el-table :data="tableData" border style="width: 100%" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55"></el-table-column>-->
      <el-table-column prop="ID" label="序号" width="60">
         <template slot-scope="scope">{{scope.$index+1}}</template>
      </el-table-column>
      <el-table-column prop="created_at" label="时间" width="180">
        <template slot-scope="scope">
          {{timeFun(scope.row.created_at)}}   
        </template>
      </el-table-column>      
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="hospital_name" label="医院"></el-table-column>
      <el-table-column prop="begin_time" label="活动时间" width="180">
        <template slot-scope="scope">
          <span>{{timeFun(scope.row.begin_time)}} 至 {{timeFun(scope.row.end_time)}}   </span>
        </template>
      </el-table-column>
      <el-table-column prop="tui" label="状态">
        <template slot-scope="scope">
          {{activeFun(scope.row)}}   
        </template>
      </el-table-column>
      <el-table-column prop="page_view" label="浏览量" width="60"></el-table-column>
      <el-table-column prop="player_count" label="参与量" width="60"></el-table-column>
      <el-table-column width="150" label="操作">
        <template slot-scope="scope">
          <div v-if="userRole.HPT_AC_1706002">
            <el-button class="addBtn" type="primary" @click="addUserFun(scope.row)">编辑</el-button>
            <el-button class="addBtn" type="warning" v-if="showUplineFun(scope.row)" @click="upStatus(scope.row.id)">下线</el-button>
            <el-button class="addBtn"  >置顶</el-button>
            <el-button class="addBtn" type="danger" @click="deletFun(scope.row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
    </div>
    </div>
     <div v-if="pushStatus">
       <Addactivity  @getlist = "getlist" ref="artice" :editActive="editActive"></Addactivity>
    </div>

  </div>
</template>

<script>
  import Addactivity from './Activitem/Addactivity'
  import Http from '@/Http/Http'
  import getTime from '@/Http/getTime'
  export default {
    data() {
      return {
        userRole:JSON.parse(sessionStorage.getItem('userSolt')),
        pushStatus:false,
        value:'关注',
        dataPush:'',
        total:0,
        currentPage4: 1,//分野  
        page:1,
        pageSize:10,
        tableData: []
      }
    },
    components:{
      Addactivity
    },
    created(){
      this.getlist()
    },
    methods: {
      getlist(){
        Http.getActivityslList({
          "pageIndex":this.page,
          "pageSize":this.pageSize
        })
         .then( res => {
          this.tableData = res.data.data.rows;
          this.total = res.data.data.total;
        })
        .catch( error => {
          
        })
        this.pushStatus = false;
      },
      
      //添加用户
      addUserFun(row) {
        if (row) {
          this.editActive = row;
        } else {
          this.editActive = '';
        };
        this.pushStatus = true
      },
      timeFun(row) {
        return getTime(row)
      },
      showUplineFun(row){
        if(row.status == 2) return false;
        const data = Date.parse(new Date())
        if (data > row.end_time) { 
          return false
        } else {
          return true
        }
      },
      activeFun(row) {
        if(row.status == 2){
          return '活动结束'
        }
        const data = Date.parse(new Date())
        if (row.begin_time > data) { 
          return '活动未开始'
        } else if (row.begin_time < data && data < row.end_time) {
          return '活动中'
        } else if (data > row.end_time) {
          return '活动结束'
        }
      },
      upStatus(id){
        Http.editActive({
          'id':id,
          'status':2
        })
        .then( res => {
          if( res.data.code == 200 ){
            this.$message.success('活动下线成功')
            this.getlist()
          } else if(res.data.code == -1003) {
            this.$message.error(`请求失败原因：${res.data.message}`)
            this.$router.push('/')
          }
        })
        .catch( error => {
          this.$message.error('服务器请求失败')
        })
      },
      deletFun(id) {
        Http.deletActivity(id)
        .then( res => {
          if(res.data.code == 200) {
            this.$message.success('删除成功')
          } else {
            this.$message.error('删除失败')
          }
        })
        .catch( err => {
          this.$message.error('服务器请求失败')
        })
      },
      //选择 推荐
      handleSelectionChange() {
        
      },
      //分页
      handleSizeChange(val) {
        this.pageSize = val;
        this.getlist()
        
      },
      handleCurrentChange(val) {
        this.page = val;
        this.getlist()
        
      },
    }
  }
</script>
<style  scoped>
.box{
  border-bottom:1px solid red;
}
    .test{
        border:1px solid #ccc;
    }
    .drag-item{
        width: 200px;
        height: 50px;
        line-height: 50px;
        margin: auto;
        position: relative;
        
        background: #ddd;
        margin:20px 5px;
    }
    .ghostClass{
        opacity: 1;
    }
    .bottom{
        width: 200px;
        height: 50px;
        position: relative;
        background: blue;
        top:2px;
        left: 2px;
        transition: all .5s linear;
    }
</style>
